<?php
/**
 * 
 * @author tangbl
 *
 */
class ActyhjAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index(){
		
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("Actyhj");
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "isdel=2 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			
			$upd = L('cfg_list_itf_btn_modify');
			$del = L('cfg_list_itf_btn_delete');
			foreach($list AS $row){
				if($row['type']=='discount') {
					$type = '折扣';
				} elseif($row['type']=='cash') {
					$type = '抵现';
				} elseif($row['type']=='fullcut') {
					$type = '满减';
				} elseif($row['type']=='fullsend') {
					$type = '满送';
				} elseif($row['type']=='fulldiscount') {
					$type = '满折';
				}
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'lang'     => $row['lang'],
						'title'    => $row['title'],
						'type'     => $type,
						'nums'     => $row['totalnum'],
						//'frequency'=> $row['usetimes'],
						'expires'  => date("Y-m-d H:i:s",$row['expires']),
						'addtime'  => date('Y-m-d',$row['addtime']),
						'deal'=>'
						<a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>  
						<a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>
						<a class="btn green" href="'.__URL__.'/yhjlog/yhjid/'.$row['id'].'"><i class="icon-flickr"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			$this->display();	
		}
	}
	
	/**
	 * 添加
	 */
	public function add(){
		
		if(IS_POST){
			
			$allow = $_POST['allow'];
			if(is_array($allow)){
				$allow = json_encode($allow);
			}
			
			$param = array(
				"upload" => array("single"=>array("single")),
				"reply"  => true,
				"arr"    => array(
					"attr"      => json_encode($_POST['attr']),
					"starttime" => strtotime($this->_post("starttime")),
					"expires"   => strtotime($this->_post("expires")),
					"allow"     => $allow,
				),
			);
			parent::do_add("Actyhj",$param);
		}else{
			
			$lang    = $this->_param("lang","",getLang());			
			$sence_yhj = '';
			if(is_array(C("SENCE_YHJ"))){
				foreach(C("SENCE_YHJ") as $k=>$v){
					$sence_yhj .= '<input type="radio" name="sence" value="'.$k.'"> '.$v[0].'<br/>';
				}
			}
			
			$this->assign("lang",$lang);
			$this->assign("sence_yhj",$sence_yhj);
			
			//用户分组
			$mgroup_select = '';
			$model_mgroup = M('mgroup');
			$data_mgroup = $model_mgroup->select();
			if($data_mgroup){
				foreach($data_mgroup as $v){
					$mgroup_select .= '<input type="checkbox" name="allow[mgroup][select][]" value="'.$v['id'].'"/> '.$v['title'].'&nbsp;&nbsp;&nbsp;';
				}				
			}
			$this->assign("mgroup_select",$mgroup_select);
			
			//用户等级
			$mlevel_select = '';
			$model_mlevel = M('mlevel');
			$data_mlevel = $model_mlevel->select();
			if($model_mlevel){
				foreach($data_mlevel as $v){
					$mlevel_select .= '<input type="checkbox" name="allow[mlevel][select][]" value="'.$v['id'].'"/> '.$v['title'].'&nbsp;&nbsp;&nbsp;';
				}				
			}
			$this->assign("mlevel_select",$mlevel_select);
			
			$this->display();
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		$param = array(
			"reply"  => true
		);
		parent::do_del("Actyhj",false,$param);
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		if(IS_POST){
			$allow = $_POST['allow'];
			if(is_array($allow)){
				$allow = json_encode($allow);
			}
			
			$param = array(
				"upload" => array("single"=>array("single")),
				"reply"  => true,
				"arr"    => array(
					"attr"      => json_encode($_POST['attr']),
					"starttime" => strtotime($this->_post("starttime")),
					"expires"   => strtotime($this->_post("expires")),
					"allow"   => $allow,
				),
			);
			parent::do_edit("Actyhj",$param);
		}else{
			
			$model   = D("Actyhj");
			$id      = $this->_param('id','intval',0);
			if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
			
			$data = $model->find($id); 
			$arr  = json_decode($data['attr'],true);
			
			$starttime = date("Y-m-d H:i:s",$data['starttime']);
			$expires   = date("Y-m-d H:i:s",$data['expires']);
			
			$single = find_thumb($data['solevar'],'single',$module);
			
			$sence_yhj = '';
			if(is_array(C("SENCE_YHJ"))){
				foreach(C("SENCE_YHJ") as $k=>$v){
					if($data['sence']==$k){
						$sence_yhj .= '<input type="radio" name="sence" value="'.$k.'" checked/> '.$v[0].'<br/>';
					}else{
						$sence_yhj .= '<input type="radio" name="sence" value="'.$k.'"/> '.$v[0].'<br/>';
					}					
				}
			}
			
			$allow = json_decode($data['allow'],true);
			$this->assign("allow",$allow);
			
			//用户分组
			$mgroup_old = @$allow['mgroup']['select'];
			if(is_array($mgroup_old)){
				$mgroup_old = ','.implode(',',$mgroup_old);
			}
			$mgroup_select = '';
			$model_mgroup = M('Mgroup');
			$data_mgroup = $model_mgroup->select();
			if($data_mgroup){
				foreach($data_mgroup as $v){
					if(stripos('|'.$mgroup_old,','.$v['id'])){
						$mgroup_select .= '<input type="checkbox" name="allow[mgroup][select][]" value="'.$v['id'].'" checked/>'.$v['title'];
					}else{
						$mgroup_select .= '<input type="checkbox" name="allow[mgroup][select][]" value="'.$v['id'].'"/>'.$v['title'];
					}
					
				}				
			}
			$this->assign("mgroup_select",$mgroup_select);
			
			//用户等级
			$mlevel_old = @$allow['mlevel']['select'];
			if(is_array($mlevel_old)){
				$mlevel_old = ','.implode(',',$mlevel_old);
			}
			$mlevel_select = '';
			$model_mlevel = M('mlevel');
			$data_mlevel = $model_mlevel->select();
			if($model_mlevel){
				foreach($data_mlevel as $v){
					if(stripos('|'.$mlevel_old,','.$v['id'])){
						$mlevel_select .= '<input type="checkbox" name="allow[mlevel][select][]" value="'.$v['id'].'" checked/>'.$v['title'].'&nbsp;&nbsp;&nbsp;';
					}else{
						$mlevel_select .= '<input type="checkbox" name="allow[mlevel][select][]" value="'.$v['id'].'"/>'.$v['title'].'&nbsp;&nbsp;&nbsp;';
					}
				}				
			}
			$this->assign("mlevel_select",$mlevel_select);
			
			$this->assign("single",$single);
			$this->assign("starttime",$starttime);
			$this->assign("expires",$expires);
			$this->assign("attr",$arr);
			$this->assign("actyhj",$data);
			$this->assign("sence_yhj",$sence_yhj);
			
			$this->display();	
		}	
	}
	
	/**
	 * 优惠卷领取记录列表
	 */
	public function yhjlog() {
		
		if(IS_AJAX){
			import('ORG.Util.Page');
			$model     = D("ActyhjCoupon");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			//$yhjid     = $this->_get("yhjid","intval",0);
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "1=1 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$del       = L('cfg_list_itf_btn_delete');
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'lang'     => $row['lang'],
						'id'       => $row['id'],
						'yhjno'    => $row['yhjno'],
						'realname' => $row['realname'],
						'mobile'   => $row['mobile'],
						'status'   => $row['status']==2 ? '未消费' : '已消费',
						'addtime'  => date('Y-m-d',$row['addtime']),
						'deal'     => '<a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> '
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			
			$yhjid = $this->_get("yhjid","intval",0);		
			$this->assign("yhjid",$yhjid);
			
			$data = M("Actyhj")->find($yhjid);
			$this->assign("detail",$data);
			$this->display();
		}
	}
	
	/**
	 * 物理删除优惠卷领取记录
	 */
	public function dellog(){
		
		parent::do_del("ActyhjCoupon",true);	
	}
}
?>